<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Classroom;
use app\admin\model\Classifymodel;
use app\admin\validate\Content as validate;

class Classify extends Common
{
    protected $croommodel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->validateType = 'classify';
        $this->localModel = new Classifymodel();
        parent::__construct($app);
        $this->croommodel = new Classroom();
    }

    public function index()
    {
        $where = $showDataArr = [];
        $fid = $this->request->request('fid', '');

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['name'])    $where[] = ['c.name', 'like', "%".$dataArr['name']."%"];
        if($dataArr['cid'])     $where[] = ['cid', '=', $dataArr['cid']];
        if($dataArr['status'])  $where[] = ['c.status', '=', $dataArr['status']];
        if(session('adminUserInfo.cid'))  $where[] = ['cid', 'in', session('adminUserInfo.cid').', 0'];
        if($fid){
            $where[] = ['c.fid', '=', $fid];
            $where[] = ['c.level', '=', 2];
            $detail = $this->localModel->findDetaiInfoById('name, desc', $fid);
        }else{
            $where[] = ['c.level', '=', 1];
        }

        $list = $this->localModel->findAll($where, $this->request->param());
        $showDataArr = ['infoData'=>$list['data'], 'pageData'=>$list['page'], 'dataArr'=>$dataArr, 'fid'=>$fid, 'detail'=>$detail];
        View::assign($showDataArr);
        return view();
    }

    public function add(){
        $id  = $this->request->get('id', 0);

        $croomData = $this->croommodel->findAllInfo('id, name', [['status', '=', 1]]);

        $showData = ['croomData'=>$croomData];
        $showData['fid'] = $this->request->get('fid', 0);
        if($showData['fid']){
            $fdetail = $this->localModel->findDetaiInfoById('cid', $showData['fid']);
            $showData['cid'] = $fdetail['cid'];
        }
        if($id){
            $showData['id'] = $id;
            $showData['detail'] = $this->localModel->findDetaiInfoById('*', $id);
            View::assign($showData);
            return view('edit');
        }else{
            View::assign($showData);
            return view();
        }
    }

    /**
     * 单表信息全局新增|编辑
     * @return \think\response\Json
     */
    public function save(){
        $dataArr = $this->request->request('info/a', []);
        $id = $this->request->post('id', 0);

        if($msg = $this->checkData($dataArr, $this->validateType))  return json(['result'=>'fail', 'title'=>$msg]);

        $res = $this->localModel->saveDataInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add'];

        if($this->request->post('fid') || $dataArr['fid']){
            return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('classify/index', ['fid'=>$dataArr['fid'] ? $dataArr['fid'] : $this->request->post('fid')])]);
        }else{
            return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('classify/index')]);
        }
    }

    public function del(){
        $id = $this->request->post('id', 0);

        if(!$id)  return json(array('result'=>'fail', 'title'=>'参数不能为空！'));

        //删除前确认是否有下级分类
        $res = $this->localModel->findAllInfo('id', [['fid', '=', $id]]);
        if($res)  return json(array('result'=>'fail', 'title'=>'该分类下有子分类，请先删除子分类！'));

        //删除角色
        $delRes = $this->localModel->del($id);
        if(!$delRes)  return json(array('result'=>'fail', 'title'=>'操作失败！'));
        $this->logsData =  ['cid'=>$id, 'action'=>'del'];

        return json(array('result'=>'success'));
    }

    public function ajaxGetCatList(){
        $fid = $this->request->post('fid');

        if(!$fid)  return json(['result'=>'fail', 'title'=>'参数fid不存在！']);

        $detail = $this->localModel->findAllInfo('id, name', [['level', '=', 2], ['fid', '=', $fid], ['status', '=', 1]]);

        if(!$detail)  return json(['result'=>'success']);
        return json(['result'=>'success', 'date'=>$detail]);
    }

}
